//
//  ViewController.swift
//  day02
//
//  Created by 杨绍鑫 on 2018/9/5.
//  Copyright © 2018年 crcnet. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let titLable : UILabel = {
        let lable = UILabel.init()
        lable.font = UIFont.systemFont(ofSize: 18)
        lable.textColor = UIColor.white
        lable.text = "Costom Font"
        lable.textAlignment = .center
        return lable
    }()
    
    let desc = [" 30 Days Swift ",
                " 这些字体特别适合打「奋斗」和「理想」",
                " 谢谢「造字工房」，本案例不涉及商业使用",
                " 使用到造字工房劲黑体，致黑体，童心体",
                " 呵呵，再见🤗 See you next Project"]
    
    let tableView : UITableView = {
        let tab = UITableView.init()
        tab.rowHeight = 40
        tab.backgroundColor = UIColor.black
        tab.tableFooterView = UIView.init()
        tab.tableHeaderView = UIView.init()
        return tab
    }()
    
    
    let chanageButton : UIButton = {
        let button = UIButton.init()
        button.setTitle("Chanage Font", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.layer.cornerRadius = 50.0
        button.clipsToBounds = true
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.backgroundColor = UIColor.yellow
        return button
    }()
    
    var fontNames = ["MFTongXin_Noncommercial-Regular",
                     "MFJinHei_Noncommercial-Regular",
                     "MFZhiHei_Noncommercial-Regular",
                     "Zapfino",
                     "Gaspar Regular"]
    
    var fontIndex = 0;
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.setAction()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setupUI()  {
        self.view.addSubview(titLable)
        self.view.addSubview(tableView)
        self.view.addSubview(chanageButton)
        self.view.backgroundColor = UIColor.black

        
        titLable.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 50)
        tableView.frame = CGRect.init(x: 0, y: 50, width: self.view.frame.width, height: 200)
        chanageButton.frame = CGRect.init(x: (self.view.frame.size.width - 100) * 0.5, y: self.view.frame.size.height - 150, width: 100, height: 100)
        
        tableView.dataSource = self
        
        
    }
    
    func setAction(){
        chanageButton.addTarget(self, action: #selector(changeFont), for: .touchUpInside)
    }
    
    @objc func changeFont() {
        fontIndex = (fontIndex + 1) % 5
        print(fontNames[fontIndex])
        tableView.reloadData()
    }

}
extension ViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return desc.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let identify = "cell___"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: identify)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: identify)
        }
        cell!.backgroundColor = UIColor.black
        cell!.textLabel?.text = desc[indexPath.row]
        cell!.textLabel?.textColor = UIColor.white
        
        cell!.textLabel?.font = UIFont.init(name: fontNames[fontIndex] , size: 16)
        return cell!
    }
}

